import 'package:flutter/material.dart';
import 'package:mybase/base/widget/layout/FFContainer.dart';
import 'package:mybase/base/widget/progress/FFProgressManager.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

class FFProgressItem extends StatefulWidget {
  String id;
  EdgeInsetsGeometry? margin;
  EdgeInsetsGeometry? padding;
  double lineHeight;
  Color? progressColor;
  Color? backgroundColor;
  bool finishHide;
  Widget? finishWidget;

  FFProgressItem({
    super.key,
    required this.id,
    this.margin,
    this.padding,
    this.lineHeight = 4.0,
    this.progressColor,
    this.backgroundColor,
    this.finishHide = true,
    this.finishWidget,
  });

  @override
  State<FFProgressItem> createState() => _FFProgressItemState();
}

class _FFProgressItemState extends State<FFProgressItem> {
  FFNotifier? vo;

  double? lastPercent;

  @override
  void initState() {
    super.initState();
    vo = FFProgressManager().getValueNotifier(widget.id);

    assert(vo != null,
        "vo不能为空，你需要提前设置值，不然会报错，使用后面的代码在合适的位置 FFProgressManager().setValueNotifier('这儿填对应的id', TypeTransferNotifier(TransferNotifierVo(type: TransferNotifierVoType.progress)));");
    //FFProgressManager().setValueNotifier("这儿填对应的id", TypeTransferNotifier(TransferNotifierVo(type: TransferNotifierVoType.progress)));
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<FFNotifierVo>(
        valueListenable: vo!,
        builder: (BuildContext context, FFNotifierVo value, Widget? child) {
          if (value.type == FFNotifierVoType.finish && widget.finishHide) {
            return widget.finishWidget ?? Container();
          }
          return FFContainer(
            margin: widget.margin,
            padding: widget.padding,
            child: LinearPercentIndicator(
              padding: EdgeInsets.zero,
              animation: true,
              lineHeight: widget.lineHeight,
              animateFromLastPercent: true,
              percent: value.type == FFNotifierVoType.finish ? 1.0 : value.getPercent(),
              progressColor: widget.progressColor,
              backgroundColor: widget.backgroundColor,
              animationDuration: value.getPercent() < 0.999999 ? 1000 : 800,
            ),
          );
        });
  }
}
